* * * TOP INCOME SHARES IN SWITZERLAND * * * 
 
 * Isabel Martinez, University of St. Gallen, 2012
 // this version: July 2016
 

* PREPARE SEPARATE DATA SETS FOR THE FIGURES, WHICH ARE PRODUCED IN R *

// ssc install sencode
	* this is used to generate a string variable with the canton name to later create labels based on this

	

* fig1
use "$thepath/4a_TopIncomeShares_CH_1981-2010.dta", clear

keep if (cant == 00 | cant==12 | cant==91 | cant==92 | cant==93)
keep cant year P99d

sort cant year
by cant: ipolate P99d year, generate (P99d_ipol) 
gen P99d_ipol_missing = P99d_ipol
foreach var in P99d_ipol_missing P99d {
replace `var' = . if year> 1993 & year<2002 & cant==0
replace `var' = . if year> 1995 & year<1999 & cant==92
replace `var' = . if year> 1997 & year<2001 & cant==93
replace `var' = . if year> 1997 & year<2003 & cant==91
}

sdecode cant, gen(kanton)
replace kanton = "Switzerland" if cant==0

saveold "$thepath/results/figures/fig1.dta", version(12) replace


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig2
use "$thepath/4a_TopIncomeShares_CH_1981-2010.dta", clear
keep if cant == 00

keep year P99d P999d P10_1d
ipolate P99d year, generate (P99d_ipol) 
ipolate P999d year, generate (P999d_ipol) 
ipolate P10_1d year, generate (P10_1d_ipol) 
gen top1 = "Top 1%"
gen top01 = "Top 0.1%"
gen top10_1 = "Top 10-1%"


saveold "$thepath/results/figures/fig2.dta", version(12) replace

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig3
use "$thepath/4b_TopIncomeShares_CH_1933-2010.dta", clear
keep year P90d P95d P99d P995d P999d  P10_5d P5_1d

// rename and prepare to reshape dataset
rename P10_5d P105
	label var P105 "Top 10-5%"
rename P5_1d P51 
	label var P51 "Top 5-1%"
	
foreach var in P90 P95 P99 P995 P999  {
rename `var'd `var'
}

gen P501=P995-P999
	label var P501 "Top 0.5-0.1%"

gen P2105=P99-P995
	label var P2105 "Top 1-0.5%"

//reshape
reshape long P group, i(year) j(id)
sort id year
by id: ipolate P year, generate (topshare_ipol) 
rename P topshare


sort id year

// write a label
drop group
gen group = "Top 10%" if id ==90
replace group = "Top 5%" if id ==95
replace group = "Top 1%" if id ==99
replace group = "Top 0.50%" if id ==995
replace group = "Top 0.10%" if id ==999
replace group = "Top 10-5%" if id ==105
replace group = "Top 5-1%" if id ==51
replace group = "Top 0.5-0.1%" if id ==501
replace group = "Top 1-0.5%" if id ==2105	


saveold "$thepath/results/figures/fig3.dta", version(12) replace

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig4
use "$thepath/4b_TopIncomeShares_CH_1933-2010.dta", clear
keep year top10_within_top1  top10_within_top10

rename top10_within_top1 top101
rename top10_within_top10 top1010

reshape long top group, i(year) j(id)
sort id year
by id: ipolate top year, generate (topshare_ipol) 
rename top topshare

sort id year

// write a label
drop group
gen group = "Top 0.1% within Top 1%" if id ==101
replace group = "Top 1% within Top 10%" if id ==1010

saveold "$thepath/results/figures/fig4.dta", version(12) replace


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig5
import excel "$thepath/Data.xlsx", sheet("fig5") cellrange(A2:F393) clear firstrow
drop if year<1913
foreach var in top1 top001 top10 top01 {
replace `var'=`var'/100
}
tempfile international
save "`international'"

use "$thepath/4b_TopIncomeShares_CH_1933-2010.dta", clear
keep year P9999d  P99d P90d
gen country = "Switzerland"
rename P9999d top001
rename P99d top1
rename P90d top10

append using "`international'"

bys country: ipolate top1 year, generate (top1_ipol) 
bys country: ipolate top001 year, generate (top001_ipol) 
bys country: ipolate top10 year, generate (top10_ipol) 


saveold "$thepath/results/figures/fig5.dta", version(12) replace

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig6
use "$thepath/4a_TopIncomeShares_CH_1981-2010.dta", clear
keep if cant==00 | cant==98 | cant==99
replace P99d = synth_P99d if cant==0
replace P999d = synth_P999d if cant==0

keep cant year P99d P999d 

sort cant year
by cant: ipolate P99d year, generate (P99d_ipol) 
by cant: ipolate P999d year, generate (P999d_ipol)
 
gen type = "Imputation" if cant== 98
replace type = "OLS" if cant== 99
replace type = "Synthetic Control" if cant==0

saveold "$thepath/results/figures/fig6", version(12) replace

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig7
/*
The raw data for Figure 7 is confidential. The Stata file fig7.dta (contained in the figures-folder) is simply the 
information in Online Appendix Table 7 pasted into a Stata data set.
*/
use "$thepath/fig7.dta", clear
saveold "$thepath/results/figures/fig7.dta", version(12) replace

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig8, fig9, fig10
use "$thepath/5b_TopLaborShares_1981-2010.dta", clear
gen cant=0

merge m:1 year cant using "$thepath/4a_TopIncomeShares_CH_1981-2010.dta"
drop if _merge==2
drop _merge
drop cant

keep year cat P99d P999d P99_ahv P999_ahv 

// bring the dataset in correct shape
foreach var in P99 P999 {
rename `var'_ahv `var'_
rename `var'd `var'_0
}

reshape wide P99_ P999_ , i(year) j(cat)
reshape long P99_ P999_ , i(year) j(cat)

foreach var in P99 P999 {
rename `var'_ `var'
}

label define workercategory 0 `"Total income (tax units)"', modify
label define workercategory 1 `"Employees"', modify
label define workercategory 2 `"Self-employed"', modify
label define workercategory 3 `"Labor income (employees and self-employed)"', modify

label values cat workercategory
sort cat year

// gen a string variable with the categories to label the graphs correctly
	// ssc install sencode
	// ssc install sdecode
sdecode cat, gen(category)	
label drop workercategory

// interpolate missing years (in the biennial tax data)
sort cat year
by cat: ipolate P99 year, generate (P99_ipol)

// gen growth in top shares (Figure 10)
bys cat: gen change_P99_ipol = (P99_ipol-P99_ipol[1])/P99_ipol[1] + 1
bys cat: gen change_P99 = (P99-P99[1])/P99[1] + 1


saveold "$thepath/results/figures/fig8_fig9_fig10.dta", version(12) replace

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig11
set scheme mygraphs
* MARKETCAPITALIZATION
import excel "$thepath/Data.xlsx", sheet("fig11") cellrange(B2:J34) clear firstrow case(lower)
tempfile market
save "`market'"

use "$thepath/5b_TopLaborShares_1981-2010.dta", clear
keep if cat==3
keep year P99_ahv P90_ahv P999_ahv

merge 1:1 year using "`market'"
drop _merge

foreach var in P99_ahv P90_ahv P999_ahv {
replace `var' = `var'*100
}

label var P99_ahv "Top 1% labor income share"
label var P90_ahv "Top 10% labor income share"
label var P999_ahv "Top 0.1% labor income share"
label var year "Year"
label var deu "GER"
tsset year

/*
use "/Users/isabel/PhD/Dropbox/Top Income Shares/Calculations/toplabor_market_correlations/marketcapitalization.dta", clear
replace top1_labor = top1_labor*100
label var top1_labor "Top 1% Labor Income"
label var year "Year"
label var deu "GER"
tsset year
*/

pwcorr P99_ahv che deu fra usa gbr lux nld, sig
pwcorr P99_ahv L.che L.deu L.fra L.usa L.gbr L.lux L.nld L.dnk, sig
pwcorr P99_ahv L2.che L2.deu L2.fra L2.usa L2.gbr L2.lux L2.nld L2.dnk, sig
pwcorr P99_ahv L3.che L3.deu L3.fra L3.usa L3.gbr L3.lux L3.nld L3.dnk, sig


* draw connected line plot
macro drop gr c

replace lux=. if year==1988
local gr `gr' line P99_ahv year,  yaxis(1) lcolor(gs0) lwidth(0.9) lpattern(dash)||
local gr `gr' (line che year, yaxis(2) lcolor(gs0) lwidth(0.9) ) ||

local usa_l msize(1.2) msymbol(O) lcolor(gs12) mcolor(gs12)
local gbr_l msize(1.2) msymbol(D) lcolor(gs12) mcolor(gs12)
local lux_l msize(1.4) msymbol(T) lcolor(gs8) mcolor(gs8)
local nld_l msize(1.2) msymbol(S) lcolor(gs8) mcolor(gs8)
local fra_l msize(1.8) msymbol(Oh) lcolor(gs5) mcolor(gs5)
local deu_l msize(2.0) msymbol(X) lcolor(gs5) mcolor(gs5)


foreach c in   usa gbr lux nld fra deu {
local gr `gr' (connected `c' year, yaxis(2) ``c'_l' ) ||
}


mylabels 5(1)9, myscale(@/1) suffix(" %") local(myla)
graph twoway `gr', legend(col(2) symxsize(10) symysize(3) size(4)  keygap(1) ring(0) position(11) bmargin(medsmall)) ///
 graphregion(color(white)) bgcolor(white) ///
ytitle("Market Capitalization in % of GDP", axis(2) size(4.5)) ytitle("Top 1% Labor Income Share in Switzerland", axis(1) size(4.5)) ylabel(`myla',  gstyle(dot) axis(1)) ///
 xlabel(1980(2)2012, labsize(medium) grid gstyle(dot) ) xtitle( , size(4)) xmtick(1980(1)2012) xsize(18) ysize(8)
graph export "$thepath/results/figures/figure11-marketcapitalization_bw.pdf", as(pdf) replace



* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig12

use "$thepath/7a_TopWealthShares_CH_1913-2011.dta", clear
keep year P05_01 P1_05 P10_5 P5_1 P999 P99

rename P99		wealthsh1
rename P5_1		wealthsh2
rename P10_5	wealthsh3
rename P999		wealthsh4
rename P05_01	wealthsh5
rename P1_05	wealthsh6

reshape long wealthsh, i(year) j(gr)

xtset gr year
tsfill, full

sort gr year
by gr: ipolate wealthsh year, generate (wealthsh_ipol) 

gen group = 	"Top 1%" 		if gr==1
replace group = "Top 5-1%" 		if gr==2
replace group = "Top 10-5%" 	if gr==3
replace group = "Top 0.1%" 		if gr==4
replace group = "Top 0.5-0.1%" 	if gr==5
replace group = "Top 1-0.5%" 	if gr==6

saveold "$thepath/results/figures/fig12.dta", version(12) replace

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* fig13

use "$thepath/7b_TopWealthShares_CH_1913-2011-corrected.dta", clear
keep year P90 aws2_P90 aws3_P90 P99 aws2_P99 aws3_P99 P999 aws2_P999 aws3_P999 pct_covered

/* 	in 1930s, low coverage of wealth (around 62% compared to >80% in other periods) 
	and extremely low share of tax units reporting positive wealth (around 4% compared to 15-80% in other periods) 
	one may exclude the corresponding years from graph, as they are of lower data quality than all other years:
	
foreach var in P90 aws2_P90 aws3_P90 P99 aws2_P99 aws3_P99 P999 aws2_P999 aws3_P999 {
replace `var'=. if pct_covered <0.8
}
*/


rename P90			wealthsh1
rename aws2_P90		wealthsh2
rename aws3_P90		wealthsh3
rename P99			wealthsh4
rename aws2_P99		wealthsh5
rename aws3_P99		wealthsh6
rename P999			wealthsh7
rename aws2_P999	wealthsh8
rename aws3_P999	wealthsh9

reshape long wealthsh, i(year) j(gr)

xtset gr year
tsfill, full

sort gr year
by gr: ipolate wealthsh year, generate (wealthsh_ipol) 

gen group = 	"Top 10%" 									if gr==1
replace group = "Top 10% incl. pension wealth (avg.)" 		if gr==2
replace group = "Top 10% incl. pension wealth (prop.)" 		if gr==3
replace group = "Top 1%" 									if gr==4
replace group = "Top 1% incl. pension wealth (avg.)" 		if gr==5
replace group = "Top 1% incl. pension wealth (prop.)" 		if gr==6
replace group = "Top 0.1%" 									if gr==7
replace group = "Top 0.1% incl. pension wealth (avg.)" 		if gr==8
replace group = "Top 0.1% incl. pension wealth (prop.)" 	if gr==9

saveold "$thepath/results/figures/fig13.dta", version(12) replace


* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* END * * END * * END * * END * * END * * END * * END * * END * * END * * END * 

